<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>使用用户自定义函数对数组中的每个元素做回调处理</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.array-walk" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_walk</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">array_walk</span> &mdash; <span class="dc-title">使用用户自定义函数对数组中的每个元素做回调处理</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.array-walk-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_walk</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$array</code></span>
   , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$userdata</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span>
  ] ) : <span class="type">bool</span></div>

  <p class="simpara">
   将用户自定义函数 <code class="parameter">funcname</code> 应用到
   <code class="parameter">array</code> 数组中的每个单元。
  </p>
  <p class="para">
   <span class="function"><strong>array_walk()</strong></span> 不会受到 <code class="parameter">array</code>
   内部数组指针的影响。<span class="function"><strong>array_walk()</strong></span>
   会遍历整个数组而不管指针的位置。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.array-walk-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">array</code></dt>

     <dd>

      <p class="para">
       输入的数组。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">callback</code></dt>

     <dd>

      <p class="para">
       典型情况下
   <code class="parameter">callback</code> 接受两个参数。<code class="parameter">array</code>
   参数的值作为第一个，键名作为第二个。
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
       如果 <code class="parameter">callback</code> 需要直接作用于数组中的值，则给
    <code class="parameter">callback</code> 的第一个参数指定为<a href="language.references.html" class="link">引用</a>。这样任何对这些单元的改变也将会改变原始数组本身。
       </p>
      </p></blockquote>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
        <p class="para">
         参数数量超过预期，传入内置函数 (例如 <span class="function"><a href="strtolower.html" class="function">strtolower()</a></span>)，
         将抛出警告，所以不适合当做 <code class="parameter">funcname</code>。
        </p>
      </p></blockquote>
      <p class="para">
       只有 <code class="parameter">array</code> 的值才可以被改变，用户不应在回调函数中改变该数组本身的结构。例如增加/删除单元，unset
   单元等等。如果 <span class="function"><strong>array_walk()</strong></span>
   作用的数组改变了，则此函数的的行为未经定义，且不可预期。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">userdata</code></dt>

     <dd>

      <p class="para">
       如果提供了可选参数
   <code class="parameter">userdata</code>，将被作为第三个参数传递给
   callback <code class="parameter">funcname</code>。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.array-walk-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>

 <div class="refsect1 errors" id="refsect1-function.array-walk-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
      如果 <code class="parameter">callback</code> 函数需要的参数比给出的多，则每次
   <span class="function"><strong>array_walk()</strong></span> 调用 <code class="parameter">callback</code>
   时都会产生一个 <a href="errorfunc.constants.html" class="link">E_WARNING</a>
   级的错误。
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.array-walk-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6082">
    <p><strong>Example #1 <span class="function"><strong>array_walk()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$fruits&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"d"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"lemon"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"orange"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"b"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"banana"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"c"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"apple"</span><span style="color: #007700">);<br /><br />function&nbsp;</span><span style="color: #0000BB">test_alter</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$item1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$prefix</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$item1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$prefix</span><span style="color: #DD0000">:&nbsp;</span><span style="color: #0000BB">$item1</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">test_print</span><span style="color: #007700">(</span><span style="color: #0000BB">$item2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$key</span><span style="color: #DD0000">.&nbsp;</span><span style="color: #0000BB">$item2</span><span style="color: #DD0000">&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />}<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Before&nbsp;...:\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">array_walk</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'test_print'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">array_walk</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'test_alter'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"...&nbsp;and&nbsp;after:\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">array_walk</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'test_print'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Before ...:
d. lemon
a. orange
b. banana
c. apple
... and after:
d. fruit: lemon
a. fruit: orange
b. fruit: banana
c. fruit: apple
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.array-walk-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="array_walk_recursive.html" class="function" rel="rdfs-seeAlso">array_walk_recursive()</a> - 对数组中的每个成员递归地应用用户函数</span></li>
    <li class="member"><span class="function"><a href="iterator_apply.html" class="function" rel="rdfs-seeAlso">iterator_apply()</a> - 为迭代器中每个元素调用一个用户自定义函数</span></li>
    <li class="member"><span class="function"><a href="list.html" class="function" rel="rdfs-seeAlso">list()</a> - 把数组中的值赋给一组变量</span></li>
    <li class="member"><span class="function"><a href="each.html" class="function" rel="rdfs-seeAlso">each()</a> - 返回数组中当前的键／值对并将数组指针向前移动一步</span></li>
    <li class="member"><span class="function"><a href="call_user_func_array.html" class="function" rel="rdfs-seeAlso">call_user_func_array()</a> - 调用回调函数，并把一个数组参数作为回调函数的参数</span></li>
    <li class="member"><span class="function"><a href="array_map.html" class="function" rel="rdfs-seeAlso">array_map()</a> - 为数组的每个元素应用回调函数</span></li>
    <li class="member"><a href="language.pseudo-types.html#language.types.callback" class="link">callback</a> 类型的信息</li>
    <li class="member"><a href="control_structures.foreach.html" class="link">foreach</a></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>